package svgedit.svg;

import java.awt.Shape;
import java.awt.geom.Ellipse2D;

import svgedit.gui.Marker.MarkerType;

/**
 * Element describing a circle.
 * 
 * Specified in 9.8.2 Interface SVGCircleElement
 */
public class SVGCircleElement extends SVGStylableElement implements SVGDrawable
{

	/** Circle's radius	*/
	private SVGLength r;
	
	/** Circle's centre x coordinate */
	private SVGLength cx;

	/** Circle's centre y coordinate */
	private SVGLength cy;

	/**
	 * Constructs a Circle of 0 size at coordinates 0,0
	 * @see svgedit.svg.SVGLength 
	 * @param document
	 */
	public SVGCircleElement(SVGDocument document)
	{
		super(document);

		r = new SVGLength(document, SVGLength.SVG_DIMENSION_OTHER);
		cx = new SVGLength(document, SVGLength.SVG_DIMENSION_X);
		cy = new SVGLength(document, SVGLength.SVG_DIMENSION_Y);
	}

	@Override
	public void acceptVisitor(SVGVisitor visitor)
	{
		visitor.visitCircle(this);
	}

	@Override
	public Shape createShape()
	{
		double cxValue = cx.getValue();
		double cyValue = cy.getValue();
		double rValue = Math.abs(r.getValue());
		return new Ellipse2D.Double(cxValue - rValue, cyValue - rValue,
				rValue * 2, rValue * 2);
	}

	@Override
	public void drag(double deltaX, double deltaY, MarkerType mType)
	{
		SVGCircleElement circleElement = (SVGCircleElement) this;
		double cx = circleElement.getCX().getValue();
		double cy = circleElement.getCY().getValue();
		double r = circleElement.getR().getValue();

		// Get the marker type
		switch (mType)
		{
			case MOVE:
				circleElement.getCX().setValueFromLength(
						new SVGLength(SVGLength.SVG_LENGTHTYPE_PX,
								(float) (cx - deltaX)));

				circleElement.getCY().setValueFromLength(
						new SVGLength(SVGLength.SVG_LENGTHTYPE_PX,
								(float) (cy - deltaY)));

				break;

			case GROW:
				circleElement.getR().setValueFromLength(
						new SVGLength(SVGLength.SVG_LENGTHTYPE_PX,
								(float) (r - deltaX)));

				break;
		}
	}

	/**
	 * Gets the center X coordinate
	 * @return the center X coordinate
	 */
	public SVGLength getCX()
	{
		return cx;
	}

	/**
	 * Gets the center Y coordinate
	 * 
	 * @return the center Y coordinate
	 */
	public SVGLength getCY()
	{
		return cy;
	}

	/**
	 * Gets the radius length
	 * @return the radius length
	 */
	public SVGLength getR()
	{
		return r;
	}

	@Override
	public Shape getSelectionStroke()
	{
		Shape strokedShape = super.getSelectionStroke();

		// Move
		this.addMarker(strokedShape, this.getBounds().getCenterX(), this
				.getBounds().getCenterY(), MarkerType.MOVE);

		// Resize
		this.addMarker(strokedShape, this.getBounds().getX()
				+ this.getBounds().getWidth(), this.getBounds().getCenterY(),
				MarkerType.GROW);

		return strokedShape;
	}

	@Override
	public void write(int indent)
	{
		System.out.printf("%" + indent + "s", " ");
		System.out.printf("<circle id='%s'>\n", this.toString());

	}
}
